#include "GameState.h"


GameState::GameState()
{	
	m_players = new Player[2];
	m_players[0].setNumber(0);
	m_players[1].setNumber(1);
}


GameState::~GameState()
{
}

void GameState::startState(std::vector< std::vector<char> >& grid)
{
}
void GameState::movePiece(std::vector< std::vector<char> >& grid, int fromX, int fromY, int toX, int toY)
{
	int opponent = 0;
	bool found = false;
	bool kill = false;
	if (m_currentPlayer == 1)
	{
		opponent = 0;
	}
	else
	{
		opponent = 1;
	}

	std::vector<Piece*> pieces = m_players[m_currentPlayer].getPieces();
	std::vector<Piece*> opponentPieces = m_players[opponent].getPieces();

	for (unsigned int i = 0; i < opponentPieces.size(); ++i)
	{
			if (opponentPieces[i]->getPosX() == toX && opponentPieces[i]->getPosY() == toY) {
				kill = true;
			}
	}
	
	for (unsigned int i = 0; i < pieces.size(); ++i)
	{
		if (pieces[i]->getPosX() == fromX && pieces[i]->getPosY() == fromY)
		{
			found = true;
			if (kill)
			{
				m_players[opponent].delPiece(i);
			}

			m_moves.fromX = fromX;
			m_moves.fromY = fromY;
			m_moves.kill = kill;
			m_moves.player = m_currentPlayer;
			m_moves.toX = toX;
			m_moves.toY = toY;
			m_moves.retract = false;

			grid[fromY][fromX] = '.';
			pieces[i]->setPosX(toX);
			pieces[i]->setPosY(toY);
			grid[toY][toX] = pieces[i]->getSymbol();
			m_numMoves++;
			m_currentPlayer = opponent;
			break;
		}
	}

	if (!found)
	{
		std::cout << "There is no piece in location (" << fromX << ", " << fromY << ") belonging to player " 
			      << m_currentPlayer << '\n';
	}
}
int GameState::getNumMoves()
{
	return m_numMoves;
}
void GameState::printPieces()
{
	std::cout << m_players[0].getNumPieces() << "  ";
	std::cout << m_players[1].getNumPieces() << '\n';
}
void GameState::retract(std::vector< std::vector<char> >& grid)
{
	if(m_numMoves > 0)
	{
		if (!m_moves.retract)
		{
			m_currentPlayer = m_moves.player;
			std::vector<Piece*> pieces = m_players[m_currentPlayer].getPieces();
			int opponent;
			if(m_currentPlayer == 1)
				opponent = 0;
			else if(m_currentPlayer == 0)
				opponent = 1;
			for (unsigned int i = 0; i < pieces.size(); ++i)
			{
				if (pieces[i]->getPosX() == m_moves.toX && pieces[i]->getPosY() == m_moves.toY)
				{
					grid[m_moves.toY][m_moves.toX] = '.';
					pieces[i]->setPosX(m_moves.fromX);
					pieces[i]->setPosY(m_moves.fromY);
					grid[m_moves.fromY][m_moves.fromX] = pieces[i]->getSymbol();
					m_numMoves--;
					break;
				}
			}
			if (m_moves.kill == true)
			{
				std::vector<Piece*> opponentPieces = m_players[opponent].getPieces();
				char symb = opponentPieces[0]->getSymbol();
				m_players[opponent].addPiece(symb, m_moves.toX, m_moves.toY);
				grid[m_moves.toY][m_moves.toX] = symb;
			}
			m_moves.retract = true;
		}
		else
			std::cout << "You have already retracted" << '\n';
	}
	else 
		std::cout << "No moves to retract" << '\n';
}

int GameState::getEvaluation()
{
	int value = -m_players[m_currentPlayer].getNumPieces();
	if (m_currentPlayer == 1)
	{
		value = (-1)*value;
	}
	return value;
}